package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Assert;
import cn.hutool.http.HttpStatus;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.enums.LindiTradeApplicationFileEnum;
import com.minimalist.common.entity.lindi.po.LindiTradeApplicationFile;
import com.minimalist.common.entity.lindi.vo.tradeapplicationfile.LindiTradeApplicationFileQueryVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationfile.LindiTradeApplicationFileVO;
import com.minimalist.common.feign.client.basic.FileFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.lindi.mapper.LindiTradeApplicationFileMapper;
import com.minimalist.lindi.service.LindiTradeApplicationFileService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 林地指标交易平台_交易申请相关文件 服务层实现。
 *
 * @author 11987
 * @since 2025-04-27
 */
@Service
public class LindiTradeApplicationFileServiceImpl extends
        ServiceImpl<LindiTradeApplicationFileMapper, LindiTradeApplicationFile>
        implements LindiTradeApplicationFileService{

    private static final Logger log = LoggerFactory.getLogger(LindiTradeApplicationFileServiceImpl.class);

    @Autowired
    private LindiTradeApplicationFileMapper mapper;

    @Autowired
    private FileFeignClient fileFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTradeApplicationFile(LindiTradeApplicationFileVO fileVO){
        LindiTradeApplicationFile file = BeanUtil.copyProperties(fileVO
                , LindiTradeApplicationFile.class);
        file.setTradeApplicationFileId(UnqIdUtil.uniqueId());
        int insert = mapper.insert(file, true);
        Assert.isTrue(insert == 1, () -> new BusinessException(
                LindiTradeApplicationFileEnum.ErrorMsgEnum.ADD_TRADE_APPLICATION_FILE_ERROR
                        .getDesc()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTradeApplicationFileByTradeApplicationFileId(
            LindiTradeApplicationFileVO fileVO){
        LindiTradeApplicationFile file = mapper
                .selectTradeApplicationFileByTradeApplicationFileId(fileVO
                        .getTradeApplicationFileId());
        Assert.notNull(file, () -> new BusinessException(LindiTradeApplicationFileEnum
                .ErrorMsgEnum.NONENTITY_TRADE_APPLICATION_FILE.getDesc()));
        BeanUtil.copyProperties(fileVO, file, CopyOptions.create().ignoreNullValue());
        int update = mapper.update(file);
        Assert.isTrue(update == 1, () -> new BusinessException(
                LindiTradeApplicationFileEnum.ErrorMsgEnum
                        .UPDATE_TRADE_APPLICATION_FILE_ERROR.getDesc()));
    }

    @Override
    public PageResp<LindiTradeApplicationFileVO> getPageTradeApplicationFileList(
            LindiTradeApplicationFileQueryVO queryVO){
        //分页查询
        Page<LindiTradeApplicationFile> filePage = mapper
                .selectPageTradeApplicationFileList(queryVO);
        //数据转换
        List<LindiTradeApplicationFileVO> indexVOList = BeanUtil.copyToList(
                filePage.getRecords(), LindiTradeApplicationFileVO.class);
        return new PageResp<>(indexVOList, filePage.getTotalRow());
    }

    @Override
    public LindiTradeApplicationFileVO getTradeApplicationFileByTradeApplicationFileId(Long tradeApplicationFileId){
        LindiTradeApplicationFile file = mapper
                .selectTradeApplicationFileByTradeApplicationFileId(
                        tradeApplicationFileId);
        Assert.notNull(file, () -> new BusinessException(LindiTradeApplicationFileEnum
                .ErrorMsgEnum.NONENTITY_TRADE_APPLICATION_FILE.getDesc()));
        return BeanUtil.copyProperties(file, LindiTradeApplicationFileVO.class);
    }

    @Override
    public List<LindiTradeApplicationFileVO> getTradeApplicationFilesByApplicationId(
            Long tradeApplicationId){
        List<LindiTradeApplicationFile> files = mapper
                .selectTradeApplicationFilesByTradeApplicationFileId(tradeApplicationId);
        return BeanUtil.copyToList(files, LindiTradeApplicationFileVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTradeApplicationFileByTradeApplicationFileId(Long tradeApplicationFileId){
        LindiTradeApplicationFile file = mapper
                .selectTradeApplicationFileByTradeApplicationFileId(tradeApplicationFileId);
        Assert.notNull(file, () -> new BusinessException(LindiTradeApplicationFileEnum
                .ErrorMsgEnum.NONENTITY_TRADE_APPLICATION_FILE.getDesc()));
        int delete = mapper.delete(file);
        Assert.isTrue(delete == 1, () -> new BusinessException(
                LindiTradeApplicationFileEnum.ErrorMsgEnum
                        .DELETE_TRADE_APPLICATION_FILE_ERROR.getDesc()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFileByFileId(Long fileId){
        try{
            R<Void> result = fileFeignClient.deleteFile(fileId);
            if (result.getCode() != HttpStatus.HTTP_OK){
                log.warn("删除文件失败，文件ID: {}, 错误信息: {}", fileId, result.getMessage());
            }
        } catch (Exception e){
            log.warn("删除文件异常，文件ID: {}, 异常信息: {}", fileId, e.getMessage());
        }
    }
}
